

#import "BaseRequest.h"
#import "NSMutableDictionary+Addition.h"

@implementation BaseRequest
@synthesize  delegate = _delegate;
@synthesize dataDict = _dataDict;
@synthesize binaryDataArray = _binaryDataArray;
@synthesize fileDataArray = _fileDataArray;
@synthesize requestName = _reqestName;
@synthesize method = _method;
@synthesize requestId;
@synthesize request = _request;
@synthesize context = _context;
@synthesize version = _version;
@synthesize url = _url;

-(id)init
{
    self = [super init];
    if (self != nil) {
        _dataDict = [[NSMutableDictionary alloc] init];
        _binaryDataArray = [[NSMutableArray alloc] init];
        _fileDataArray = [[NSMutableArray alloc] init];
        _method = HTTP_GET;
        [_method retain];
        self.requestId = 0;
        
        _url = PROTOCOL_URL;
    }
    return self;
}

- (void)dealloc
{
    self.dataDict = nil;
    self.binaryDataArray = nil;
    self.fileDataArray = nil;
    self.requestName = nil;
    self.method = nil;
    self.request = nil;
    self.context = nil;
    self.delegate = nil;
    self.url = nil;
    [super dealloc];
}


-(void)setRequestProtocolName:(NSString*)protocolname
{
    self.requestName = protocolname;
}
-(void)setRequestHttpGet
{
    self.method = @"GET";
}
-(void)setRequestHttpPost
{
    self.method = @"POST";
}
-(void)addStringValue:(NSString*)value forKey:(NSString*)key
{
    if(value==nil || key==nil)
        return;
    [_dataDict setObject:value forKey:key];
}

-(void)addDictionaryValue:(NSDictionary*)dict
{
    if (!dict || ![dict isKindOfClass:[NSDictionary class]])
    {
        return;
    }
    NSArray *keys = [dict allKeys];
    for (int i=0; i<[keys count]; i++) 
    {
        NSString *key = [keys objectAtIndex:i];
        NSString *object = [dict objectForKey:key];
        [self addStringValue:object forKey:key];
    }
}

-(NSString*)getStringValueForKey:(NSString*)key
{
    if(key==nil)
        return @"";
    return [_dataDict objectStringForKey:key defaultValue:@""];
}
-(void)addBinaryValue:(NSData*)value forKey:(NSString*)key withFileName:(NSString*)filename withMIMEtype:(NSString*)mimetype
{
    if(value==nil || key==nil)
        return;
    NSMutableDictionary* dict = [NSMutableDictionary dictionary];
    [dict setObject:key forKey:DATA_KEY];
    [dict setObject:value forKey:BIANRY_DATA_KEY];
    if(filename!=nil)
    {
        [dict setObject:filename forKey:FILENAME_KEY];
    }
    if(mimetype!=nil)
    {
        [dict setObject:mimetype forKey:MIME_KEY];
    }
    [_binaryDataArray addObject:dict];
}
-(void)addFilePathValue:(NSString*)value forKey:(NSString*)key withFileName:(NSString*)filename withMIMEtype:(NSString*)mimetype
{
    if(value==nil || key==nil)
        return;
    NSMutableDictionary* dict = [NSMutableDictionary dictionary];
    [dict setObject:key forKey:DATA_KEY];
    [dict setObject:value forKey:FILE_PATH_KEY];
    if(filename!=nil)
    {
        [dict setObject:filename forKey:FILENAME_KEY];
    }
    if(mimetype!=nil)
    {
        [dict setObject:mimetype forKey:MIME_KEY];
    }
    [_fileDataArray addObject:dict];
}

-(void)printRequest
{
    NSObject* key;
    int dictcount;
    NSEnumerator *dataEnumerator;
    
    NSLog(@"-------start printRequest");
    NSLog(@"requestName:%@",_reqestName);
    NSLog(@"requestMethod:%@",_method);
    
    dictcount = [_dataDict count];
    NSLog(@"DictValues has %d values:",dictcount);
    dataEnumerator = [_dataDict keyEnumerator];
    while ((key = [dataEnumerator nextObject])) {
        NSLog(@"\tkey:%@\t\tvalue:%@",key,[_dataDict objectForKey:key]);
    }
    
    dictcount = [_binaryDataArray count];
    NSLog(@"BianryValues has %d values:",dictcount);
    for(NSDictionary* dict in _binaryDataArray)
    {
        NSLog(@"\tkey:%@\t\tfilename:%@\t\tmimetype:%@",[dict objectForKey:DATA_KEY],[dict objectForKey:FILENAME_KEY],[dict objectForKey:MIME_KEY]);
    }
    
    dictcount = [_fileDataArray count];
    NSLog(@"FileValues has %d values:",dictcount);
    for(NSDictionary* dict in _fileDataArray)
    {
        NSLog(@"\tkey:%@\t\tfilename:%@\t\tmimetype:%@",[dict objectForKey:DATA_KEY],[dict objectForKey:FILENAME_KEY],[dict objectForKey:MIME_KEY]);
    }

    NSLog(@"-------end printRequest");
}

@end
